<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use App\Http\Models\Friend;

class FriendController extends AdminBaseController{

    protected $type = ['1'=>'friend','2'=>'brand','3'=>'media','4'=>'imgfriend','5'=>'hotcompany'];//后台当前栏目页面对应

    public function __construct(Request $request){
        parent::__construct($request);
    }




    public function index(Request $request,$type)
    {
        //data不可数
        $data = Friend::where('type',$type)->orderBy("created_at","desc")->get();
        //$data = $data->paginate(20);
        $this->param['data'] = $data;
        $this->param['active'] = $this->type[$type];
        return view("admin.friend.index",$this->param);
    }

    public function create(Request $request,$type)
    {
        $this->param['type'] = $type;
        $this->param['active'] = $this->type[$type];
        return view('admin.friend.create',$this->param);
    }

    public function store(Request $request){
        $friend = new Friend;
        $friend -> title = $request->input('title');
        $friend -> url = $request -> input('url');
        $friend -> cover = $request ->input('cover');
        $friend -> type = $request ->input('type');

        $friend -> save();

        return redirect('/administrator/friend/type/'.$friend->type);
    }

    public function edit($id){

        $data = Friend::find($id);

        $this->param['data'] = $data;
        $this->param['type'] = $data->type;
        $this->param['active'] = $this->type[$data->type];
        return view('admin.friend.edit',$this->param);
    }


    public function update(Request $request,$id)
    {
        $friend = Friend::find($id);

        $friend -> title = $request->input('title');
        $friend -> url = $request -> input('url');
        $friend -> cover = $request ->input('cover');
        $friend -> type = $request ->input('type');


        $friend->save();

        return redirect('/administrator/friend/type/'.$friend->type);
    }

    public function delete($id)
    {
        $friend = Friend::find($id);
        $type = $friend->type;
        $res = Friend::destroy($id);
        if($res) return view('success',['url'=>'/administrator/friend/type/'.$type]);

        return view('failure',['url'=>'/administrator/friend/type/'.$type]);
    }

}